package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;

/**
 * 按照primaryKey查询
 * @author Administrator
 *
 */
public class SelectOneByDynamicMethod extends AbstractCreateInterfaceMethod{


	private static final String METHOD_NAME="selectOneByDynamic";
	
	private boolean isUseSession = false ;
	
	public SelectOneByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	public SelectOneByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn,
			boolean  isUseSession
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		this.isUseSession = isUseSession;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {	
//		IfBlock ifBlock = new IfBlock(new StringBuilder()
//			.append(DaoConstant.CONDITION_VARIABLE_NAME)
//			.append(" == null")
//			.toString()
//		);
//		ifBlock.addBodyLine("return null");
//		method.addBlocks(ifBlock);
		IfBlock ifBlock = null;
		if( isUseSession()){
			ifBlock = new IfBlock(new StringBuilder().append( DaoConstant.SQL_SESSION_NAME ).append(" == null").toString());
			ifBlock.addBodyLine("return null");
			method.addBlocks(ifBlock);
		}
		ifBlock = new IfBlock(new StringBuilder()
			.append(DaoConstant.CONDITION_VARIABLE_NAME)
			.append(" == null") 
			.append( " || "+ DaoConstant.CONDITION_VARIABLE_NAME +".getOredCriteria().size() == 0" )
			.toString()
		);
		ifBlock.addBodyLine(DaoConstant.CONDITION_VARIABLE_NAME+" = null");
		method.addBlocks(ifBlock);
		
		method.addBodyLine(
				new StringBuilder()
				.append(JavaKeyWord.RETURN).append(JavaKeyWord.OPERATION_BRACKET_LEFT)
				.append(method.getReturnType().getFullName())
				.append(JavaKeyWord.OPERATION_BRACKET_RIGHT)
				//.append("super.")
				.append(mybatisPlugin.getDependentModel().call(null))
				.append("selectOne ( ")
				.append( isUseSession()? DaoConstant.SQL_SESSION_NAME+" , ":""  )
				//.append(DaoConstant.SELECT_BY_DYNAMIC_FIELD_NAME)
				.append(super.getMapperId().getSelectByDynamicMapperId())
				.append(" , ")
				.append(DaoConstant.CONDITION_VARIABLE_NAME).append(")")
				.toString()
		);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {		
		String answer = null;
		if( !isUseSession ){
			answer = METHOD_NAME;
		}else if( isUseSession  && mybatisPlugin.isEnableUseSqlSession() ){
			answer = METHOD_NAME;
		}
		return answer;
	}

	/**
	 * @return the isUseSession
	 */
	@Override
	public final boolean isUseSession() {
		return isUseSession;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		String[] answer = new String[]{
			"动态条件查询一条记录。"
		};
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		
		return super.getEntityClassType(introspectSchema.getSchema());
	}

}
